home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / src / loadmod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-25  |  31.6 KB  |  932 lines

  1. /*      loadmod.c
  2.  *
  3.  * Generic Module Player Protracker Module loader
  4.  *
  5.  * $Id: loadmod.c,v 1.8 1997/01/25 15:23:16 pekangas Exp $
  6.  *
  7.  * Copyright 1996,1997 Housemarque Inc.
  8.  *
  9.  * This file is part of the MIDAS Sound System, and may only be
  10.  * used, modified and distributed under the terms of the MIDAS
  11.  * Sound System license, LICENSE.TXT. By continuing to use,
  12.  * modify or distribute this file you indicate that you have
  13.  * read the license and understand and accept it fully.
  14. */
  15.  
  16. #include "lang.h"
  17. #include "mtypes.h"
  18. #include "errors.h"
  19. #include "mglobals.h"
  20. #include "mmem.h"
  21. #include "file.h"
  22. #include "sdevice.h"
  23. #include "gmplayer.h"
  24. #ifndef NOEMS
  25. #include "ems.h"
  26. #endif
  27. #include "mutils.h"
  28.  
  29. RCSID(const char *modload_rcsid = "$Id: loadmod.c,v 1.8 1997/01/25 15:23:16 pekangas Exp $";)
  30.  
  31.  
  32. /* Macro for endianness-swap. DANGEROUS - references the argument x
  33.    twice */
  34. #define SWAP16(x) ( ((x << 8) & 0xFF00) | ( (x >> 8) & 0x00FF) )
  35.  
  36. /* Pass error code in variable "error" on, used in gmpLoadMOD(). */
  37. #define PASSERR() { LoadError(); PASSERROR(ID_gmpLoadMOD) }
  38.  
  39.  
  40. /****************************************************************************\
  41. *       struct modInstHdr
  42. *       -----------------
  43. * Description:  Protracker module instrument header. Note that all 16-bit
  44. *               fields are big-endian.
  45. \****************************************************************************/
  46.  
  47. typedef struct
  48. {
  49.     char        iname[22];              /* instrument name */
  50.     ushort      slength;                /* sample length */
  51.     uchar       finetune;               /* sample finetune value */
  52.     uchar       volume;                 /* sample default volume */
  53.     ushort      loopStart;              /* sample loop start, in words */
  54.     ushort      loopLength;             /* sample loop length, in words */
  55. } modInstHdr;
  56.  
  57.  
  58.  
  59.  
  60. /****************************************************************************\
  61. *       struct modHeader
  62. *       ----------------
  63. * Description:  Protracker module file header
  64. \****************************************************************************/
  65.  
  66. typedef struct
  67. {
  68.     char        songName[20];           /* song name */
  69.     modInstHdr  instruments[31];        /* instrument headers */
  70.     uchar       songLength;             /* song length */
  71.     uchar       restart;                /* unused by Protracker, song restart
  72.                                            position in some modules */
  73.     uchar       songData[128];          /* pattern playing orders */
  74.     char        sign[4];                /* module signature */
  75. } modHeader;
  76.  
  77.  
  78.  
  79.  
  80. /* Period table for Protracker octaves 0-5: */
  81. static unsigned ptPeriods[6*12] = {
  82.     1712,1616,1524,1440,1356,1280,1208,1140,1076,1016,960,907,
  83.     856,808,762,720,678,640,604,570,538,508,480,453,
  84.     428,404,381,360,339,320,302,285,269,254,240,226,
  85.     214,202,190,180,170,160,151,143,135,127,120,113,
  86.     107,101,95,90,85,80,75,71,67,63,60,56,
  87.     53,50,47,45,42,40,37,35,33,31,30,28 };
  88.  
  89.  
  90. /* Conversion table from Protracker command number to GMP ones: */
  91. static uchar ptCommands[16] =
  92. {
  93.     gmpcArpeggio,               /* 0 - Arpeggio */
  94.     gmpcSlideUp,                /* 1 - Portamento up */
  95.     gmpcSlideDown,              /* 2 - Portamento down */
  96.     gmpcTonePortamento,         /* 3 - Tone portamento */
  97.     gmpcVibrato,                /* 4 - Vibrato */
  98.     gmpcTPortVSlide,            /* 5 - Tone portamento + volume slide */
  99.     gmpcVibVSlide,              /* 6 - Vibrato + volume slide */
  100.     gmpcTremolo,                /* 7 - Tremolo */
  101.     gmpcSetPanning,             /* 8 - Set panning (extension) */
  102.     gmpcSampleOffset,           /* 9 - Sample offset */
  103.     gmpcVolumeSlide,            /* A - Volume slide */
  104.     gmpcPositionJump,           /* B - Position jump */
  105.     gmpcSetVolume,              /* C - Set volume */
  106.     gmpcPatternBreak,           /* D - Pattern break */
  107.     gmpcNone,                   /* E - E-commands (see below) */
  108.     gmpcSetSpeed,               /* F - Set speed/tempo */
  109.  
  110. };
  111.  
  112. /* Conversion table from Protracker E-command numbers to GMP commands: */
  113. static uchar ptECommands[16] =
  114. {
  115.     gmpcNone,                   /* E0 - Toggle filter */
  116.     gmpcFineSlideUp,            /* E1 - Fine portamento up */
  117.     gmpcFineSlideDown,          /* E2 - Fine portamento down */
  118.     gmpcNone,                   /* E3 - Set glissando control */
  119.     gmpcNone,                   /* E4 - Set vibrato type */
  120.     gmpcNone,                   /* E5 - Set finetune */
  121.     gmpcPatternLoop,            /* E6 - Pattern loop */
  122.     gmpcNone,                   /* E7 - Set tremolo type */
  123.     gmpcSetPanning16,           /* E8 - Set panning (extension) */
  124.     gmpcPTRetrig,               /* E9 - Retrig note */
  125.     gmpcFineVolSlideUp,         /* EA - Fine volume slide up */
  126.     gmpcFineVolSlideDown,       /* EB - Fine volume slide down */
  127.     gmpcNoteCut,                /* EC - Note cut */
  128.     gmpcNoteDelay,              /* ED - Note delay */
  129.     gmpcPatternDelay,           /* EE - Pattern delay */
  130.     gmpcNone                    /* EF - Invert loop */
  131. };
  132.  
  133.  
  134.  
  135. /****************************************************************************\
  136. *       Module loader buffers and file handle. These variables are static
  137. *       instead of local so that a separate deallocation can be used which
  138. *       will be called before exiting in error situations
  139. \****************************************************************************/
  140. static fileHandle f;                    /* file handle for module file */
  141. static int      fileOpened;             /* 1 if module file has been opened */
  142. static gmpModule *module;               /* pointer to GMP module structure */
  143. static modHeader *header;               /* pointer to PT module header */
  144. static uchar    *instUsed;              /* instrument used flags */
  145. static int      extOctaves;             /* 1 if extended octaves are needed */
  146. static uchar    *ptPatt;                /* pointer to PT pattern data */
  147. static gmpPattern *convPatt;            /* pointer to converted pattern data*/
  148. static uchar    *smpBuf;                /* sample loading buffer */
  149. static unsigned numChans;               /* number of channels in module */
  150.  
  151.  
  152.  
  153.  
  154.  
  155. /****************************************************************************\
  156. *
  157. * Function:     int ConvertPattern(uchar *srcPatt, gmpPattern *destPatt,
  158. *                   unsigned *convLen);
  159. *
  160. * Description:  Converts a pattern from Protracker to GMP internal format.
  161. *               Also updates extOctaves and instUsed flags.
  162. *
  163. * Input:        uchar *srcPatt          pointer to pattern in PT format
  164. *               gmpPattern *destPatt    pointer to destination GMP pattern
  165. *               unsigned *convLen       pointer to converted pattern length
  166. *
  167. * Returns:      MIDAS error code. Converted pattern length (in bytes,
  168. *               including header) is written to *convLen.
  169. *
  170. \****************************************************************************/
  171.  
  172. static int ConvertPattern(uchar *srcPatt, gmpPattern *destPatt,
  173.     unsigned *convLen)
  174. {
  175.     uchar       *src = srcPatt;
  176.     uchar       *dest = ((uchar*)destPatt) + sizeof(gmpPattern);
  177.     unsigned    row, chan;
  178.     unsigned    period, len;
  179.     int         i;
  180.     uchar       note, inst, command, infobyte, compInfo;
  181.     int         legalCommand;
  182.  
  183.     for ( row = 0; row < 64; row++ )
  184.     {
  185.         for ( chan = 0; chan < numChans; chan++ )
  186.         {
  187.             /* Get period number from pattern data: */
  188.             period = ((unsigned) (*(src) & 0x0F) << 8) | ((unsigned)
  189.                 *(src+1));
  190.  
  191.             /* Get instrument number from pattern data: */
  192.             inst = ((*src) & 0xF0) | (*(src+2) >> 4);
  193.  
  194.             /* Get command number from pattern data: */
  195.             command = *(src+2) & 0x0F;
  196.  
  197.             /* Get command infobyte from pattern data: */
  198.             infobyte = *(src+3);
  199.  
  200.             /* Point src to next channel pattern data: */
  201.             src += 4;
  202.  
  203.             /* Set compression info to zero to mark currently there is no
  204.                new data: */
  205.             compInfo = 0;
  206.  
  207.             /* Check if there is a new note: */
  208.             note = 0;
  209.             if ( period != 0 )
  210.             {
  211.                 /* Find the that corresponds to current period value: */
  212.                 for ( i = 0; i < 6*12; i++ )
  213.                 {
  214.                     if ( period >= ptPeriods[i] )
  215.                     {
  216.                         note = i;
  217.                         break;
  218.                     }
  219.                 }
  220.  
  221.                 /* If we reached the end of period table, the pattern data
  222.                    is invalid: */
  223.                 if ( i == (6*12) )
  224.                     return errInvalidPatt;
  225.  
  226.                 /* Convert note number to GMP internal format: */
  227.                 note = ((note / 12) << 4) | (note % 12);
  228.  
  229.                 /* Check if extended octaves should be enabled: */
  230.                 if ( (note < 0x10) || (note > 0x3F) )
  231.                     extOctaves = 1;
  232.  
  233.                 /* Mark that there is a new note: */
  234.                 compInfo |= 32;
  235.             }
  236.  
  237.             /* Check if there is a new instrument: */
  238.             if ( inst != 0 )
  239.             {
  240.                 /* Check that the instrument number is legal: */
  241.                 if ( inst > 31 )
  242.                     return errInvalidPatt;
  243.  
  244.                 /* Mark instrument used: */
  245.                 instUsed[inst-1] = 1;
  246.  
  247.                 /* Mark that there is a new instrument: */
  248.                 compInfo |= 32;
  249.             }
  250.  
  251.             /* Check if there is a command: */
  252.             if ( (command != 0) || (infobyte != 0) )
  253.             {
  254.                 /* Flag that we have a legal command: */
  255.                 legalCommand = 1;
  256.  
  257.                 switch ( command )
  258.                 {
  259.                     case 0x08:
  260.                         /* Command 8 - possibly Set Panning. Convert to
  261.                            "Set Panning" -command if the infobyte is a legal
  262.                            DMP-compatible panning value: */
  263.                         if ( (infobyte <= 0x80) || (infobyte == 0xA4) )
  264.                         {
  265.                             command = gmpcSetPanning;
  266.                         }
  267.                         else
  268.                         {
  269.                             legalCommand = 0;
  270.                         }
  271.                         break;
  272.  
  273.                     case 0x0E:
  274.                         /* Command E - convert command number: */
  275.                         command = ptECommands[infobyte >> 4];
  276.                         infobyte = infobyte & 0x0F;
  277.                         break;
  278.  
  279.                     case 0x0F:
  280.                         /* Protracker command 0Fh - set speed or set tempo.
  281.                            If infobyte > 32, the BPM tempo is changed: */
  282.                         if ( infobyte > 32 )
  283.                             command = gmpcSetTempo;
  284.                         else
  285.                             command = gmpcSetSpeed;
  286.                         break;
  287.  
  288.                     default:
  289.                         /* Convert command to GMP command number: */
  290.                         command = ptCommands[command];
  291.                         break;
  292.                 }
  293.  
  294.                 /* Mark that there is a command: */
  295.                 if ( legalCommand )
  296.                     compInfo |= 128;
  297.             }
  298.  
  299.             /* If the compression information is nonzero, there is some
  300.                data for this channel: */
  301.             if ( compInfo != 0 )
  302.             {
  303.                 /* Set channel number to lower 5 bits of the compression info
  304.                    and write it to destination: */
  305.                 compInfo |= chan;
  306.                 *(dest++) = compInfo;
  307.  
  308.                 /* Check if there a note+instrument pair: */
  309.                 if ( compInfo & 32 )
  310.                 {
  311.                     /* Write note number if there is a new note, otherwise
  312.                        write 0xFF as note: */
  313.                     if ( period != 0 )
  314.                         *(dest++) = note;
  315.                     else
  316.                         *(dest++) = 0xFF;
  317.  
  318.                     /* Write instrument number if there is a new instrument,
  319.                        otherwise write 0xFF as instrument: */
  320.                     if ( inst != 0 )
  321.                         *(dest++) = inst-1;
  322.                     else
  323.                         *(dest++) = 0xFF;
  324.                 }
  325.  
  326.                 /* Check if there is a command: */
  327.                 if ( compInfo & 128 )
  328.                 {
  329.                     /* Write command and command infobyte: */
  330.                     *(dest++) = command;
  331.                     *(dest++) = infobyte;
  332.                 }
  333.             }
  334.         }
  335.  
  336.         /* Write row end marker: */
  337.         *(dest++) = 0;
  338.     }
  339.  
  340.     /* Write number of rows to pattern header: */
  341.     destPatt->rows = 64;
  342.  
  343.     /* Calculate converted pattern length: */
  344.     len = (unsigned) (dest  - ((uchar*) destPatt));
  345.  
  346.     /* Write converted pattern length to header and return it in *convLen: */
  347.     destPatt->length = len;
  348.     *convLen = len;
  349.  
  350.     return OK;
  351. }
  352.  
  353.  
  354. /****************************************************************************\
  355. *
  356. * Function:     void LoadError(void)
  357. *
  358. * Description:  Stops loading the module, deallocates all buffers and closes
  359. *               the file.
  360. *
  361. \****************************************************************************/
  362.  
  363. #define condFree(x) { if ( x != NULL ) if ( memFree(x) != OK) return; }
  364.  
  365. static void LoadError(void)
  366. {
  367.     /* Close file if opened. Do not process errors. */
  368.     if ( fileOpened )
  369.         if ( fileClose(f) != OK )
  370.             return;
  371.  
  372.     /* Attempt to deallocate module if allocated. Do not process errors. */
  373.     if ( module != NULL )
  374.         if ( gmpFreeModule(module) != OK )
  375.             return;
  376.  
  377.     /* Deallocate structures if allocated: */
  378.     condFree(header)
  379.     condFree(instUsed)
  380.     condFree(smpBuf)
  381. }
  382.  
  383.  
  384.  
  385.  
  386. /****************************************************************************\
  387. *
  388. * Function:     int gmpLoadMOD(char *fileName, int addSD, mpModule **module)
  389. *
  390. * Description:  Loads a Protracker module to memory in Generic Module Player
  391. *               module format
  392. *
  393. * Input:        char *fileName          module file name
  394. *               int addSD               1 if module samples should be added to
  395. *                                       the current Sound Device, 0 if not
  396. *               int (*SampleCallback)(...)  pointer to callback function that
  397. *                                       will be called after sample has been
  398. *                                       added to Sound Device, but before
  399. *                                       sample data is deallocated
  400. *               mpModule **module       pointer to GMP module pointer
  401. *
  402. * Returns:      MIDAS error code
  403. *
  404. \****************************************************************************/
  405.  
  406. int CALLING gmpLoadMOD(char *fileName, int addSD, int
  407.     (CALLING *SampleCallback)(sdSample *sdsmp, gmpSample *gmpsmp),
  408.     gmpModule **_module)
  409. {
  410.     int             error;              /* MIDAS error code */
  411.     modInstHdr      *modi;
  412.     gmpInstrument   *inst;
  413.     unsigned        i;
  414.     unsigned        numPatts, n;
  415.     ulong           maxSample;
  416.     static unsigned convPattLen;
  417.     static long     filePos;
  418.     unsigned        slength, loopStart, loopLength;
  419.     gmpSample       *sample;
  420.     static sdSample sdSmp;
  421. #ifndef NOEMS
  422.     uchar           *patt;
  423. #endif
  424.  
  425.  
  426.     /* point buffers to NULL and set fileOpened to 0 so that LoadError()
  427.        can be called at any point: */
  428.     fileOpened = 0;
  429.     module = NULL;
  430.     header = NULL;
  431.     instUsed = NULL;
  432.     ptPatt = NULL;
  433.     convPatt = NULL;
  434.     smpBuf = NULL;
  435.  
  436.     /* Allocate memory for Protracker module header: */
  437.     if ( (error = memAlloc(sizeof(modHeader), (void**) &header)) != OK )
  438.         PASSERR()
  439.  
  440.     /* Open module file: */
  441.     if ( (error = fileOpen(fileName, fileOpenRead, &f)) != OK )
  442.         PASSERR()
  443.     fileOpened = 1;
  444.  
  445.     /* Allocate memory for the module structure: */
  446.     if ( (error = memAlloc(sizeof(gmpModule), (void**) &module)) != OK )
  447.         PASSERR()
  448.  
  449.     /* Clear module structure so that it can safely be deallocated with
  450.        gmpFreeModule() at any point: */
  451.     module->panning = NULL;
  452.     module->songData = NULL;
  453.     module->instruments = NULL;
  454.     module->patterns = NULL;
  455.  
  456.     /* read Protracker module header: */
  457.     if ( (error = fileRead(f, header, sizeof(modHeader))) != OK )
  458.         PASSERR()
  459.  
  460.     numChans = 0;
  461.  
  462.     /* Check the module signature to determine number of channels: */
  463.     if ( mMemEqual(&header->sign[0], "M.K.", 4) )
  464.         numChans = 4;
  465.     if ( mMemEqual(&header->sign[0], "M!K!", 4) )
  466.         numChans = 4;
  467.     if ( mMemEqual(&header->sign[0], "FLT4", 4) )
  468.         numChans = 4;
  469.     if ( mMemEqual(&header->sign[0], "OCTA", 4) )
  470.         numChans = 8;
  471.  
  472.     if ( mMemEqual(&header->sign[1], "CHN", 3) )
  473.     {
  474.         /* xCHN, where x is the number of channels */
  475.         numChans = header->sign[0] - '0';
  476.     }
  477.  
  478.     if ( mMemEqual(&header->sign[2], "CH", 2) )
  479.     {
  480.         /* xxCH, where xx is the number of channels */
  481.         numChans = (header->sign[0] - '0') * 10 + (header->sign[1] - '0');
  482.     }
  483.  
  484.     if ( mMemEqual(&header->sign[0], "TDZ", 3) )
  485.     {
  486.         /* TDZx, where x is the number of channels */
  487.         numChans = header->sign[3] - '0';
  488.     }
  489.  
  490.  
  491.     /* If number of channels is undetermined, the signature is invalid. */
  492.     if ( numChans == 0 )
  493.     {
  494.         ERROR(errInvalidModule, ID_gmpLoadMOD);
  495.         LoadError();
  496.         return errInvalidModule;
  497.     }
  498.  
  499.     module->numChannels = numChans;         /* store number of channels */
  500.  
  501.     /* Copy song name: */
  502.     mMemCopy(&module->name[0], &header->songName[0], 20);
  503.     module->name[20] = 0;                   /* force terminating '\0' */
  504.  
  505.     module->songLength = header->songLength;    /* copy song length */
  506.     module->numInsts = 31;                  /* set number of instruments */
  507.     module->playMode = gmpPT;               /* set Protracker playing mode */
  508.     module->masterVolume = 64;              /* maximum master volume */
  509.     module->speed = 6;                      /* initial speed 6 */
  510.     module->tempo = 125;                    /* initial tempo 125 BPM */
  511.     module->playFlags.extOctaves = 0;       /* extended octaves not needed */
  512.  
  513.     /* Check if the header contains a valid restart position, and if it does,
  514.        use it: */
  515.     if ( (header->restart != 127) && (header->restart < header->songLength) )
  516.         module->restart = header->restart;
  517.     else
  518.         module->restart = 0;
  519.  
  520.     /* Allocate memory for channel initial panning positions: */
  521.     if ( (error = memAlloc(32 * sizeof(int), (void**) &module->panning))
  522.         != OK )
  523.         PASSERR()
  524.  
  525.     /* Set up initial panning positions: (LRRL LRRL LRRL...) */
  526.     for ( i = 0; i < numChans; i++ )
  527.     {
  528.         if ( ((i & 3) == 0) || ((i & 3) == 3) )
  529.             module->panning[i] = 0x00;
  530.         else
  531.             module->panning[i] = 0x80;
  532.     }
  533.  
  534.     /* Find the number of patterns in file by searching through the song data
  535.        to find the highest pattern number: */
  536.     numPatts = 0;
  537.     for ( i = 0; i < 128; i++ )
  538.     {
  539.         if ( header->songData[i] > numPatts )
  540.             numPatts = header->songData[i];
  541.     }
  542.     numPatts++;
  543.  
  544.     module->numPatts = numPatts;    /* store number of patterns */
  545.  
  546.     /* Allocate memory for song data: */
  547.     if ( (error = memAlloc(sizeof(ushort) * module->songLength,
  548.         (void**) &module->songData)) != OK )
  549.         PASSERR()
  550.  
  551.     /* Copy song data: */
  552.     for ( i = 0; i < module->songLength; i++ )
  553.         module->songData[i] = header->songData[i];
  554.  
  555.     /* Allocate memory for pattern pointers: */
  556.     if ( (error = memAlloc(sizeof(gmpPattern*) * module->numPatts,
  557.         (void**) &module->patterns)) != OK )
  558.         PASSERR()
  559.  
  560.     /* Set all pattern pointers to NULL to mark them unallocated: */
  561.     for ( i = 0; i < module->numPatts; i++ )
  562.         module->patterns[i] = NULL;
  563.  
  564.     /* Allocate memory for instrument pointers: */
  565.     if ( (error = memAlloc(sizeof(gmpInstrument*) * module->numInsts,
  566.         (void**) &module->instruments)) != OK )
  567.         PASSERR()
  568.  
  569.     /* Set all instrument pointers to NULL to mark them unallocated: */
  570.     for ( i = 0; i < module->numInsts; i++ )
  571.         module->instruments[i] = NULL;
  572.  
  573.     /* Allocate memory for instrument used flags: */
  574.     if ( (error = memAlloc(31, (void**)&instUsed)) != OK )
  575.         PASSERR()
  576.  
  577.     /* Mark all instruments unused: */
  578.     for ( i = 0; i < module->numInsts; i++ )
  579.         instUsed[i] = 0;
  580.  
  581.     /* Now convert all 16-bit fields in the Protracker module instrument
  582.        headers to little-endian and find maximum sample length: *!!* */
  583.     maxSample = 0;
  584.     for ( i = 0; i < module->numInsts; i++ )
  585.     {
  586.         modi = &header->instruments[i];
  587.         modi->slength = SWAP16(modi->slength);
  588.         if ( maxSample < 2L * (ulong) modi->slength )
  589.             maxSample = 2L * (ulong) modi->slength;
  590.         modi->loopStart = SWAP16(modi->loopStart);
  591.         modi->loopLength = SWAP16(modi->loopLength);
  592.     }
  593.  
  594.     /* Check that the maximum sample length is below the Sound Device limit:*/
  595.     if ( maxSample > SMPMAX )
  596.     {
  597.         ERROR(errInvalidInst, ID_gmpLoadMOD);
  598.         LoadError();
  599.         return errInvalidInst;
  600.     }
  601.  
  602.     /* Allocate memory for pattern loading buffer: */
  603.     if ( (error = memAlloc(numChans * 256, (void**) &ptPatt)) != OK )
  604.         PASSERR()
  605.  
  606.     /* Allocate memory for pattern conversion buffer: (maximum GMP pattern
  607.        data size is 6 bytes per row per channel plus header) */
  608.     if ( (error = memAlloc(numChans * 64 * 6 + sizeof(gmpPattern),
  609.         (void**) &convPatt)) != OK )
  610.         PASSERR()
  611.  
  612.     /* Load and convert all patterns: */
  613.     for ( i = 0; i < numPatts; i++ )
  614.     {
  615.         /* Read pattern data: */
  616.         if ( (error = fileRead(f, ptPatt, 256 * numChans)) != OK )
  617.             PASSERR()
  618.  
  619.         /* Convert the pattern data, checking the instruments used: */
  620.         if ( (error = ConvertPattern(ptPatt, convPatt, &convPattLen))
  621.             != OK )
  622.             PASSERR()
  623.  
  624. #ifndef NOEMS
  625.         if ( mUseEMS == 1 )             /* is EMS memory used? */
  626.         {
  627.             /* Allocate EMS for converted pattern data for current pattern in
  628.                module: */
  629.             if ( (error = emsAlloc(convPattLen, (emsBlock**)
  630.                 &module->patterns[i])) != OK )
  631.                 PASSERR()
  632.  
  633.             /* Map the allocated EMS block to conventional memory: */
  634.             if ( (error = emsMap((emsBlock*) module->patterns[i],
  635.                 (void**) &patt)) != OK)
  636.                 PASSERR()
  637.  
  638.             /* Copy the converted pattern data to the EMS block: */
  639.             mMemCopy(patt, convPatt, convPattLen);
  640.         }
  641.         else
  642. #endif
  643.         {
  644.             /* Allocate memory for converted pattern data for current pattern
  645.                in module: */
  646.             if ( (error = memAlloc(convPattLen, (void**)&module->patterns[i]))
  647.                 != OK )
  648.                 PASSERR()
  649.  
  650.             /* Copy the converted pattern data to the EMS block: */
  651.             mMemCopy(module->patterns[i], convPatt, convPattLen);
  652.         }
  653.     }
  654.  
  655.     /* Deallocate pattern conversion buffer: */
  656.     if ( (error = memFree(convPatt)) != OK )
  657.         PASSERR()
  658.     convPatt = NULL;
  659.  
  660.     /* Deallocate pattern loading buffer: */
  661.     if ( (error = memFree(ptPatt)) != OK )
  662.         PASSERR()
  663.     ptPatt = NULL;
  664.  
  665.     /* If samples should be added to Sound Device, allocate memory for sample
  666.        loading buffer: */
  667.     if ( addSD )
  668.     {
  669.         if ( (error = memAlloc(maxSample, (void**) &smpBuf)) != OK )
  670.             PASSERR()
  671.     }
  672.  
  673.     /* Get current file position: */
  674.     if ( (error = fileGetPosition(f, &filePos)) != OK )
  675.         PASSERR()
  676.  
  677.     /* Load all samples and convert sample and instrument data: */
  678.     for ( i = 0; i < module->numInsts; i++ )
  679.     {
  680.         /* Seek to the beginning of current sample: */
  681.         if ( (error = fileSeek(f, filePos, fileSeekAbsolute)) != OK )
  682.             PASSERR()
  683.  
  684.         /* Point modi to current Protracker module instrument: */
  685.         modi = &header->instruments[i];
  686.  
  687.         /* Convert sample length, loop start and loop end to bytes: */
  688.         slength = 2 * ((unsigned) modi->slength);
  689.         loopStart = 2 * ((unsigned) modi->loopStart);
  690.         loopLength = 2 * ((unsigned) modi->loopLength);
  691.  
  692.         /* Set file position to the beginning of next sample: */
  693.         filePos += (ulong) slength;
  694.  
  695.         /* Allocate memory for this instrument structure: (add space for one
  696.            sample if the instrument is used) */
  697.         if ( instUsed[i] )
  698.         {
  699.             if ( (error = memAlloc(sizeof(gmpInstrument) + sizeof(gmpSample),
  700.                 (void**) &module->instruments[i])) != OK )
  701.                 PASSERR()
  702.         }
  703.         else
  704.         {
  705.             if ( (error = memAlloc(sizeof(gmpInstrument),
  706.                 (void**) &module->instruments[i])) != OK )
  707.                 PASSERR()
  708.         }
  709.  
  710.         /* Point inst to current instrument structure: */
  711.         inst = module->instruments[i];
  712.  
  713.         /* Mark there are no sample for the instrument: */
  714.         inst->numSamples = 0;
  715.  
  716.         /* Copy instrument name: */
  717.         mMemCopy(&inst->name[0], &modi->iname[0], 22);
  718.         inst->name[22] = 0;             /* force terminating '\0' */
  719.  
  720.         /* Mark the instrument has no sample number table for keyboard
  721.            notes: */
  722.         inst->noteSamples = NULL;
  723.  
  724.         /* Mark the instrument has no volume or panning envelopes: */
  725.         inst->volEnvelope = NULL;
  726.         inst->panEnvelope = NULL;
  727.  
  728.         /* Mark the instrument has no FT2 auto-vibrato: */
  729.         inst->vibType = inst->vibSweep = inst->vibDepth = inst->vibRate = 0;
  730.  
  731.         /* Check if the instrument is used in the module: */
  732.         if ( instUsed[i] )
  733.         {
  734.             /* The instrument is used - mark it used and point sample to its
  735.                sample information: */
  736.             inst->used = 1;
  737.             inst->numSamples = 1;
  738.             sample = &inst->samples[0];
  739.  
  740.             /* Mark there is no sample data and the sample has not been added
  741.                to Sound Device: */
  742.             sample->sample = NULL;
  743.             sample->sdHandle = 0;
  744.  
  745.             /* Copy sample loop start and calculate loop end: */
  746.             sample->loop1Start = loopStart;
  747.             sample->loop1End = loopStart + loopLength;
  748.  
  749.             /* If sample loop end is past byte 2, the sample is looping:
  750.                (Protracker uses loop start 0, length 2 for no loop,
  751.                Fasttracker 1 uses start 0, length 0) */
  752.             if ( sample->loop1End > 2 )
  753.             {
  754.                 /* Looping sample - set loop types and limit sample length
  755.                    to loop end: */
  756.                 sample->loopMode = sdLoopAmiga;
  757.                 sample->loop1Type = loopUnidir;
  758.                 if ( slength > sample->loop1End )
  759.                     slength = sample->loop1End;
  760.                 sample->sampleLength = slength;
  761.             }
  762.             else
  763.             {
  764.                 /* Sample not looping: */
  765.                 sample->loopMode = sdLoopAmigaNone;
  766.                 sample->loop1Type = loopNone;
  767.                 sample->sampleLength = slength;
  768.             }
  769.  
  770.             /* Copy sample default volume: */
  771.             sample->volume = modi->volume;
  772.  
  773.             /* Copy sample finetune value: */
  774.             sample->finetune = modi->finetune;
  775.  
  776.             /* Set sample base tune to 0: */
  777.             sample->baseTune = 0;
  778.  
  779.             /* Set sample default panning position to middle: */
  780.             sample->panning = panMiddle;
  781.  
  782.             /* Check if there is sample data for this sample: */
  783.             if ( slength != 0 )
  784.             {
  785.                 /* If sample data should not be added to Sound Device, allocate
  786.                    memory for the sample data and point smpBuf there: */
  787.                 if ( !addSD )
  788.                 {
  789.                     smpBuf = NULL;
  790.                     if ( (error = memAlloc(slength, (void**)&smpBuf)) != OK )
  791.                         PASSERR()
  792.                     sample->sample = smpBuf;
  793.                 }
  794.                 else
  795.                 {
  796.                     /* Sample is added to the Sound Device - sample data is not
  797.                        available: */
  798.                     sample->sample = NULL;
  799.                 }
  800.  
  801.                 /* There is sample data - load sample: */
  802.                 if ( (error = fileRead(f, smpBuf, slength)) != OK )
  803.                     PASSERR()
  804.  
  805.                 /* Convert sample data from signed to unsigned: */
  806.                 for ( n = 0; n < slength; n++ )
  807.                     smpBuf[n] ^= 0x80;
  808.  
  809.                 /* Set Sound Device sample type: */
  810.                 sdSmp.sampleType = smp8bit;
  811.                 sample->sampleType = smp8bit;
  812.  
  813.                 /* Set Sound Device sample position in memory: */
  814.                 sdSmp.samplePos = sdSmpConv;
  815.  
  816.                 /* Point Sound Device sample data to sample loading buffer: */
  817.                 sdSmp.sample = smpBuf;
  818.  
  819.                 /* Point smpBuf to NULL if the sample is not added to Sound
  820.                    Device to mark it should not be deallocated: */
  821.                 if ( !addSD )
  822.                     smpBuf = NULL;
  823.             }
  824.             else
  825.             {
  826.                 /* Mark there is no sample data: */
  827.                 sdSmp.sampleType = smpNone;
  828.                 sample->sampleType = smpNone;
  829.                 sdSmp.samplePos = sdSmpNone;
  830.                 sdSmp.sample = NULL;
  831.             }
  832.  
  833.             sdSmp.sampleLength = slength;
  834.             sdSmp.loopMode = sample->loopMode;
  835.             sdSmp.loop1Start = sample->loop1Start;
  836.             sdSmp.loop1End = sample->loop1End;
  837.             sdSmp.loop1Type = sample->loop1Type;
  838.  
  839.  
  840.             /* Set up the rest of Sound Device sample structure so that the sample
  841.                can be added to the Sound Device: */
  842.             if ( addSD )
  843.             {
  844.                 /* Add the sample to Sound Device and store the Sound Device
  845.                    sample handle in sample->sdHandle: */
  846.                 if ( (error = gmpSD->AddSample(&sdSmp, 1, &sample->sdHandle))
  847.                     != OK)
  848.                     PASSERR()
  849.  
  850.                 /* Call sample callback if used: */
  851.                 if ( SampleCallback != NULL )
  852.                 {
  853.                     if ( (error = SampleCallback(&sdSmp, sample)) != OK )
  854.                         PASSERR()
  855.                 }
  856.             }
  857.             else
  858.             {
  859.                 /* Sample data has not been added to Sound Device: */
  860.                 sample->sdHandle = 0;
  861.             }
  862.         }
  863.         else
  864.         {
  865.             /* Sample is not used - there is no sample data: */
  866.             inst->used = 0;
  867.             inst->numSamples = 0;
  868.         }
  869.     }
  870.  
  871.     /* Deallocate sample loading buffer if allocated: */
  872.     if ( addSD )
  873.     {
  874.         if ( (error = memFree(smpBuf)) != OK )
  875.             PASSERR()
  876.     }
  877.     smpBuf = NULL;
  878.  
  879.     /* Deallocate instrument use flags: */
  880.     if ( (error = memFree(instUsed)) != OK )
  881.         PASSERR()
  882.     instUsed = 0;
  883.  
  884.     /* Set extended octaves needed flag in module header if notes in extended
  885.        octaves were found: */
  886.     if ( extOctaves )
  887.         module->playFlags.extOctaves = 1;
  888.  
  889.     /* Now we are finished loading. Close module file: */
  890.     if ( (error = fileClose(f)) != OK)
  891.         PASSERR()
  892.     fileOpened = 0;
  893.  
  894.     /* Deallocate Protracker module header: */
  895.     if ( (error = memFree(header)) != OK )
  896.         PASSERR();
  897.  
  898.     /* Return module pointer in *module: */
  899.     *_module = module;
  900.  
  901.     return OK;
  902. }
  903.  
  904.  
  905. /*
  906.  * $Log: loadmod.c,v $
  907.  * Revision 1.8  1997/01/25 15:23:16  pekangas
  908.  * The file is now closed properly if loading terminates to an error
  909.  *
  910.  * Revision 1.7  1997/01/16 18:41:59  pekangas
  911.  * Changed copyright messages to Housemarque
  912.  *
  913.  * Revision 1.6  1996/12/30 23:38:23  jpaana
  914.  * Cleaned up LoadError
  915.  *
  916.  * Revision 1.5  1996/09/01 15:42:03  pekangas
  917.  * Changed to use new style slides
  918.  *
  919.  * Revision 1.4  1996/07/13 20:11:19  pekangas
  920.  * Eliminated Visual C warnings
  921.  *
  922.  * Revision 1.3  1996/07/13 18:23:27  pekangas
  923.  * Fixed to compile with Visual C
  924.  *
  925.  * Revision 1.2  1996/05/24 16:20:36  jpaana
  926.  * Fixed to work with gcc
  927.  *
  928.  * Revision 1.1  1996/05/22 20:49:33  pekangas
  929.  * Initial revision
  930.  *
  931. */
  932.